﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Coffee_Restaurant.Database;
using MetroFramework.Controls;

namespace Coffee_Restaurant.Forms.DinnerMenus
{
    public partial class MenuToSell : UserControl
    {
        public MenuToSell()
        {
            InitializeComponent();
            LoadCategoryItems();
        }

        public void LoadCategoryItems()
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.sp_DinnerMenuCategories().ToList();
                var sumCount = db.DinnerMenus.Count();
                flowLayoutPanel1.Controls.Clear(); //xoa het nut da co san
                flowLayoutPanel1.Controls.Add(metroButton1);
                metroButton1.Text = "Tất cả (" + sumCount + ")";
                metroButton1.Click += metroButton_Click;
                int count = 1;
                foreach (var itemc in model)
                {
                    count++;
                    var metroButton = new MetroButton();
                    metroButton.FontSize = MetroFramework.MetroButtonSize.Medium;
                    metroButton.FontWeight = MetroFramework.MetroButtonWeight.Regular;
                    metroButton.Location = new System.Drawing.Point(3, 3);
                    metroButton.Name = "metroButton" + count.ToString();
                    metroButton.Size = new System.Drawing.Size(141, 50);
                    metroButton.TabIndex = 0;
                    metroButton.Text = itemc.Name;
                    metroButton.UseSelectable = true;
                    metroButton.Click += metroButton_Click;
                    //treeViewItemCategory.Nodes.Add(itemc.Name, itemc.Name + " (" + itemc.CountItems + ")");
                    flowLayoutPanel1.Controls.Add(metroButton);
                }
            }
        }

        public void LoadMenuByCategory(string name)
        {
            using (var db = new DatabaseEntities())
            {
                flowLayoutPanel2.Controls.Clear();
                var model = db.DinnerMenus.ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    model = db.DinnerMenus.Where(x => x.GroupName == name).ToList();
                }
                foreach (var dinnerMenu in model)
                {
                    var item = new DinnerMenuItem
                    {
                        metroLabel1 = { Text = dinnerMenu.Name },
                        metroLabel2 = { Text = dinnerMenu.Price.ToString() }
                    };
                    flowLayoutPanel2.Controls.Add(item);
                }

            }
        }

        private void metroButton_Click(object sender, EventArgs e)
        {
            var button = sender as MetroButton;
            if (button != null) LoadMenuByCategory(FilterTextName(button.Text));
        }

        public string FilterTextName(string text)
        {
            var array = text.Split('(');
            return array[0].Trim();
        }
    }
}
